package com.zzty.servlet;

import com.zzty.entity.Clazz;
import com.zzty.entity.Student;
import org.codehaus.jackson.map.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "/MyServlet",urlPatterns = "/MyServlet")

public class MyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        if("getClazz".equals(method)){
            getClazz(request,response);
        }else if("searchStu".equals(method)){
            searchStu(request,response);
        }else if("searchStuDetail".equals(method)){
            searchStuDetail(request,response);
        }

    }

    protected void getClazz(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Clazz> clazzs=new ArrayList<Clazz>();
        Clazz clazz=new Clazz();
        clazz.setId(1);
        clazz.setName("java1");

        Clazz clazz1=new Clazz();
        clazz1.setId(2);
        clazz1.setName("java2");

        clazzs.add(clazz);
        clazzs.add(clazz1);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(clazzs);
        PrintWriter out = response.getWriter();
        out.println(json);
        out.flush();
        out.close();
    }
    protected void searchStuDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String clazzId=request.getParameter("studentId");
        //TODO search db



        Student st=new Student();
        st.setId(1);
        st.setName("aaa");
        st.setClazzId(1);


        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(st);
        PrintWriter out = response.getWriter();
        out.println(json);
        out.flush();
        out.close();
    }

    protected void searchStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String clazzId=request.getParameter("clazzId");
        //TODO search db

        List<Student> students=new ArrayList<Student>();

        Student st=new Student();
        st.setId(1);
        st.setName("aaa");
        st.setClazzId(1);

        Student st1=new Student();
        st1.setId(1);
        st1.setName("bbb");
        st1.setClazzId(1);
        students.add(st);
        students.add(st1);

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(students);
        PrintWriter out = response.getWriter();
        out.println(json);
        out.flush();
        out.close();
    }



}
